package com.xz.group12306.servlet;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xz.group12306.business.service.Impl.UserServiceImpl;
import com.xz.group12306.domain.User;

/**
 * Servlet implementation class ChangePasswordServlet
 */
@WebServlet("/ChangePasswordServlet")
public class ChangePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChangePasswordServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 修改乱码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 获取页面的值
		String passwordold = request.getParameter("passwordold");// 原密码
		String password = request.getParameter("password");// 新密码
		String password2 = request.getParameter("password2");// 确认密码

		// 获取session值
		User user = (User) request.getSession().getAttribute("user");
		// 判断原密码是否一致
		if (user.getPassword().equals(MD5Servlet.md5(passwordold))) {
			//用正则表达式验证密码是否正确
			String regEx = "^(?=.*[0-9].*)(?=.*[a-z].*)(?=.*[A-Z].*)(?=.*\\s.*).{6,30}$";
			Pattern pattern = Pattern.compile(regEx);
			Matcher matcher = pattern.matcher(password);
			boolean rs = matcher.find();
			if (!rs) {
				response.getWriter()
						.print("<script>alert(\"新密码输入错误，修改密码失败\");window.location.href='user/index.jsp';</script>");
			} else {
				//判断两次密码是否一致
				if (password.equals(password2)) {
					// 调用方法修改密码
					UserServiceImpl userServiceImpl = new UserServiceImpl();
					userServiceImpl.updatePassword(MD5Servlet.md5(password), user.getId());
					HttpSession session = request.getSession(false);
					// 清除session值并跳转
					session.removeAttribute("user");
					response.getWriter()
							.print("<script>alert(\"修改密码成功请重新登录\");window.location.href='user/login.html';</script>");
				} else {
					response.getWriter().print(
							"<script>alert(\"两次密码输入不一致，修改密码失败\");window.location.href='user/index.jsp';</script>");
				}
			}
		} else {
			response.getWriter()
					.print("<script>alert(\"原密码输入错误，修改密码失败\");window.location.href='user/index.jsp';</script>");
		}
	}

}
